package cn.zxy.controller;

import cn.zxy.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/")
public class StudentController {

    @GetMapping("/user")
    public String user(Model model){
        model.addAttribute("name","赵生蚝");
        model.addAttribute("age",180);
        return "user";
    }

//    @RequestMapping(value = "index",method = RequestMethod.GET)
    //相当于@GetMapping("index")
    @GetMapping("index")
    public String index(){
        return "login";
    }


//    @RequestMapping(value = "login",method = RequestMethod.POST)
    //相当于@PostMapping("login")
    @PostMapping("login")
    public String Stulogin(String username,String password){
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        return "success";
    }

    @GetMapping("index2")
    public String index2(){
        return "login";
    }
    @PostMapping("login2")
    @ResponseBody
    public User Stulogin2(  User user){
        System.out.println(user.toString());
        System.out.println("username:"+user.getUsername());
        System.out.println("password:"+user.getPassword());
        return user;
    }

//    localhost:8080/save
    //http://localhost:8080/save?name=中文乱码&age=405
    @GetMapping("/save")
    @ResponseBody
    public void save(@RequestParam String name,
                     @RequestParam int age){
        System.out.println("name=="+name);
        System.out.println("age=="+age);
    }

    //初始默认值
    @GetMapping("/save2")
    @ResponseBody
    public void save2(@RequestParam(defaultValue = "赵生蚝",required = false) String name,
                     @RequestParam(defaultValue = "110",required = false) int age){
        System.out.println("name=="+name);
        System.out.println("age=="+age);
    }

    @GetMapping("/save02/{name}/{age}")
    @ResponseBody
    public void save02(@PathVariable("name") String name,
                     @PathVariable("age") int age){
        System.out.println("name=="+name);
        System.out.println("age=="+age);
    }
}
